<?php
/**
 * User: songlin
 * Date: 2017/6/19
 * 初级爬虫
 */

$timeout = 5;
$URL = 'http://www.sinoma-gdzd.cn/';
$url = $URL . 'Wygkcn_NosmallClass.asp?Wygkcn_typeid=2&Wygkcn_BigClassID=20&page=1';
$next = true;
$array = array();
do {
    //链接网站 取页面内容
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    $file_contents = curl_exec($ch);
    $file_contents = mb_convert_encoding($file_contents, "UTF-8", "GBK");
    curl_close($ch);
//file_put_contents('C:/Users/admin/Desktop/test.txt',$file_contents);

    //正则匹配 抓取数据
    preg_match_all('#<a href="(.*)" title=".*" target="_blank"> <[lb]><font color=".*"> .*</font></[lb]></a>#', $file_contents, $m);

    //写入文件中
//    $fp = fopen("C:/Users/admin/Desktop/test.txt", "a") or die("Unable to open file!");//得到指针
//    if (is_array($m[1])) {
//        foreach ($m[1] as $value) {
//            fwrite($fp, $value."\r\n");//写
//        }
//    } else {
//        fwrite($fp, $m[1] . "\r\n");//写
//    }
//    fclose($fp);

    //存入数组
    $array = array_merge($array, $m[1]);

    //判断分页情况 有下页继续循环
    preg_match_all("#<a href='([^\']*)' title='[^\']*'>下一页</A>#", $file_contents, $page);
    if (empty($page[1])) {
        $next = false;
    } else {
        $url = $URL . ($page[1][0]);
    }

} while ($next);
var_dump($array);
